package com.boot.security.server.utils.mffl.WecahtPayUtil;

import java.util.*;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 微信支付工具
 *
 * @author David Ge
 * @version 创建时间：2019年7月18日 下午4:06:18
 */
@Service
public class WechatPay {

    // 系统参数配置
    OurWxPayConfig ourWXPayConfig = new OurWxPayConfig();

    //微信支付
    WXPay wxPay = new WXPay(ourWXPayConfig);

    /**
     * NATIVE 支付下单返回支付URl
     *
     * @return
     * @throws Exception
     */
    public Map GetPayOrder(WxpayParam wxpayParam) throws Exception {
        System.out.println("访问支付");
        // 商品配置
        WxpayParam wxpayparam = new WxpayParam();

        // 参数配置类
        OurWxPayConfig ourWXPayConfig = new OurWxPayConfig();

        // 根据微信支付API来设置
        Map<String, String> data = new HashMap<String, String>();
        data.put("appid", ourWXPayConfig.getAppID());                    //公众账号ID
        data.put("mch_id", ourWXPayConfig.getMchID());                   //商户号
        data.put("trade_type", "APP");                                   //支付场景 APP 微信app支付 JSAPI 公众号支付 NATIVE 扫码支付
        data.put("notify_url", wxpayParam.getNotify_url());              //回调地址
        data.put("spbill_create_ip", wxpayParam.getSpbill_create_ip());  //终端IP
        //data.put("total_fee",wxpayParam.getTotalPrice()); 		     //标价金额
        data.put("total_fee", "1");                                      //标价金额
        data.put("out_trade_no", wxpayParam.getPayMerchantNumber());     //商户订单号
        data.put("body", wxpayParam.getBody() + "-" + wxpayParam.getPayMerchantNumber());//商品描述
        data.put("nonce_str", wxpayParam.getNonce_str());                //随机字符串
        data.put("attach", wxpayParam.getAttach());                     //附加信息
        String sign = WXPayUtil.generateSignature(data, ourWXPayConfig.getKey()); // 签名
        data.put("sign", sign);

        /* wxPay.unifiedOrder 这个方法中调用微信统一下单 */
        Map<String, String> respData = wxPay.unifiedOrder(data);

        System.out.println(respData);
        if (respData.containsKey("err_code")) {
            if (respData.get("err_code").equals("ORDERPAID") && respData.get("err_code_des").equals("该订单已支付")) {
                throw new Exception("订单已支付，无需重复支付");
            }
        }
        if (!respData.containsKey("prepay_id")) {
            return new HashMap();
        }

        //拉起支付
        Map<String, String> map1 = new LinkedHashMap<>();
        map1.put("appid", ourWXPayConfig.getAppID());
        map1.put("partnerid", ourWXPayConfig.getMchID());
        map1.put("prepayid", respData.get("prepay_id"));
        map1.put("package", "Sign=WXPay");
        map1.put("noncestr", respData.get("nonce_str"));
        map1.put("timestamp", Long.toString(System.currentTimeMillis() / 1000));

	/*	map1.put("sign", WXPayUtil.generateSignature(new HashMap<String,String>(){{
		    put("noncestr","bfrhncjkfdkfd");
		    put("prepayid", respData.get("prepay_id"));
		    put("timestamp",Long.toString(System.currentTimeMillis()/1000));
		}}, ourWXPayConfig.getKey()));*/

        map1.put("sign", WXPayUtil.generateSignature(map1, ourWXPayConfig.getKey()));


        return map1;
    }

    /**
     * 退款
     */
    public Map weChatrefund(Map<String, String> map) throws Exception {
        Map<String, String> data = new HashMap<String, String>();
        data.put("appid", ourWXPayConfig.getAppID());                   //公众账号ID
        data.put("mch_id", ourWXPayConfig.getMchID());                  //商户号
        data.put("nonce_str", "bfrhncjkfdkfd");                         //随机字符串
        //data.put("out_trade_no", wxpayParam.getPayMerchantNumber()); 	//商户订单号
        data.put("transaction_id", map.get("transaction_id"));          //微信订单号
        data.put("out_refund_no", map.get("out_refund_no"));            //商户退款单号
        //data.put("total_fee", map.get("total_fee"));                    //订单金额
        //data.put("refund_fee", map.get("refund_fee"));                  //退款金额
        data.put("total_fee", "1");                    //订单金额
        data.put("refund_fee", "1");                  //退款金额

        // data.put("notify_url", map.get("notify_url"));               //回调地址
        map.put("refund_desc", map.get("refund_desc"));                  //退款理由
        String sign = "";
        try {
            sign = WXPayUtil.generateSignature(data, ourWXPayConfig.getKey());// 签名
            System.out.println(sign);
        } catch (Exception e) {
            e.printStackTrace();

        }
        data.put("sign", sign);
        Map<String, String> refund = wxPay.refund(data);
        System.out.println(refund.toString());
        return refund;
    }

    /**
     * 微信查询订单
     */
    public Map orderquery(Map<String, String> map) throws Exception {
        Map<String, String> data = new HashMap<String, String>();
        data.put("appid", ourWXPayConfig.getAppID());             //公众账号ID
        data.put("mch_id", ourWXPayConfig.getMchID());            //商户号
        data.put("out_trade_no", map.get("out_trade_no"));        //商户单号
        data.put("nonce_str", "bfrhncjkfdkfd");                   //随机字符串
        String sign = "";
        try {
            sign = WXPayUtil.generateSignature(data, ourWXPayConfig.getKey());// 签名
            System.out.println(sign);
        } catch (Exception e) {
            e.printStackTrace();
        }
        data.put("sign", sign);
        Map<String, String> refund = wxPay.orderQuery(data);
        System.out.println(refund.toString());
        return refund;
    }

}
